<script setup lang="ts">
 import { loginApi } from '@/apis/user'
import { useUserStore } from '@/store/user'
import { ref } from 'vue'

 const store = useUserStore()

  // 表单数据
  const formData = ref({
    mobile: '13230000008',
    password: 'abc12345'
  })

  const formRef = ref(null)

    // 验证表单数据的规则
const formRules = {
    mobile: {
      rules: [
        { required: true, errorMessage: '请填写手机号码' },
        { pattern: '^1\\d{10}$', errorMessage: '手机号码格式不正确' },
      ],
    },
    password: {
      rules: [
        { required: true, errorMessage: '请输入验证码' },
        // { pattern: '^\\d{6}$', errorMessage: '验证码格式不正确' },
      ],
    },
  }

  const isAgree = ref(false)

  const onLogin = async ()=>{
    try {
    // 2.1 整体校验
    // @ts-ignore
    await formRef.value.validate()
    if (!isAgree.value) return uni.showToast({ title: '请勾选同意用户协议', icon: 'none' })

    // 2.2 调用登录接口
    const res = await loginApi(formData.value)

    // 2.3 存储token
    store.token = res.token

    // 2.4 提示用户
    uni.showToast({title: '登录成功', icon: 'success'})

    // 2.5 页面跳转(我的) --- switchTab
    uni.switchTab({
      url:'/pages/my/index',
    })
  } catch (error) {
    
  }
  }

</script>


<template>
  <uni-forms class="login-form" ref="formRef"  :model="formData" :rules="formRules">
    <uni-forms-item name="mobile">
      <uni-easyinput v-model="formData.mobile" :input-border="false" :clearable="false" placeholder="请输入手机号" placeholder-style="color: #C3C3C5" />
    </uni-forms-item>
    <uni-forms-item name="password">
      <uni-easyinput v-model="formData.password" type="password" placeholder="请输入密码" :input-border="false" placeholder-style="color: #C3C3C5" />
    </uni-forms-item>
    <view class="agreement">
      <radio :checked="isAgree" color="#16C2A3" @click="isAgree = !isAgree"/>
      我已同意
      <text class="link">用户协议</text>
      及
      <text class="link">隐私协议</text>
    </view>

    <button class="uni-button" @click="onLogin">登 录</button>
    <navigator hover-class="none" class="uni-navigator" url=" ">
      忘记密码？
    </navigator>
  </uni-forms>
</template>


<style lang="scss">
@import './styles.scss';
</style>